<?php

class company_members extends CActiveRecord {
/**
 * The followings are the available columns in table 'contact_members':
 * @var integer $id
 * @var string $type
 * @var string $person_name
 * @var string $tel
 * @var string $fax
 * @var string $email
 * @var integer $cust_id
 * @var string $mobile
 */

/**
 * Returns the static model of the specified AR class.
 * @return CActiveRecord the static model class
 */
    public $contacts,$email = array();
    public $memberType = array('In Charge','Accounts','Export','Sales','Manager');
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'company_members';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
        return array(
            array('type', 'required'),
            array('type, id, company_id', 'numerical', 'integerOnly'=>true),
            array('name','length', 'max'=>50)
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
            'company'=>array(self::BELONGS_TO, 'company_address', 'address_id'),
            'contacts'=>array(self::HAS_MANY, 'company_contacts', 'member_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
     public function validateall() {
        $validate = true;
        foreach($this->contacts as $contacts) {
            if(!$contacts->validate()) {
                $validate = false;
            }
        }
        foreach($this->email as $email) {
            if(!$email->validate()) {
                $validate = false;
            }
        }
        if(!$this->validate()) {
           $validate = false;
        }
        return $validate;
    }
    public function savemembers($valid = false) {
        $status = true;
        if(!$this->save($valid))
            $status = false;
        foreach($this->contacts as $contacts) {
            if(!empty($contacts->ref)) {
            $contacts->member_id = $this->id;
            if(!$contacts->save($valid))
                $status = false;
            }
        }
        foreach($this->email as $item) {
            if(!empty($item->ref)) {
            $item->member_id = $this->id;
            if(!$item->save($valid))
                $status = false;
            }
        }
        return $status;
    }
    public function attributeLabels() {
        return array(
        'id' => 'Id',
        'type' => 'Type',
        'person_name' => 'Name',
        'tel' => 'Telphone',
        'fax' => 'Fax No.',
        'email' => 'E-mail',
        'mobile' => 'Mobile No.',
        );
    }
}